<?php

namespace App\Domain\Account\Controllers;

use App\Domain\Account\Requests\CreateAccountPost;
use App\Domain\Account\Requests\UpdateAccountPost;
use App\Domain\Account\Services\AccountService;
use App\Http\Controllers\Controller;
use App\Supports\Constant\PaginationConst;
use App\Supports\Formatter\ResponseFormatter;
use Illuminate\Http\Request;
use Illuminate\Pagination\LengthAwarePaginator;


class AccountController extends Controller
{
    //创建用户
    public function createAccount(CreateAccountPost $request, AccountService $accountService)
    {
        $param = $request->validated();

        $res = $accountService->createAccount($param);

        return $this->success($res);
    }

    //用户列表
    public function list(Request $request, AccountService $accountService)
    {
        $this->validate($request, [
            'account' => 'string|nullable',
            'name' => 'string|nullable',
            'start_time' => 'string|nullable',
            'end_time' => 'string|nullable',
            'state' => 'integer|nullable',
            'phone' => 'string|nullable',
            'nickname' => 'string|nullable',
            'roleName' => 'string|nullable',
            'acc_start_time' => 'string|nullable',
            'acc_end_time' => 'string|nullable',
            'currentPage' => 'integer|min:1|nullable',
            'pageSize' => 'integer|min:1|nullable',
        ]);
        $param = [
            'account' => $request['account'],
            'name' => $request['name'],
            'start_time' => $request['start_time'],
            'end_time' => $request['end_time'],
            'state' => $request['state'],
            'phone' => $request['phone'],
            'nickname' => $request['nickname'],
            'roleName' => $request['roleName'],
            'acc_start_time' => $request['acc_start_time'],
            'acc_end_time' => $request['acc_end_time'],
            'currentPage' => $request->input('currentPage', 1),
            'pageSize' => $request->input('pageSize', PaginationConst::PAGE_SIZE),
        ];

        $accountPaginator = $accountService->list($param);
        $items = array_map(function ($val) {
            $val['id'] = strval($val['id']);
            return $val;
        }, $accountPaginator->items());
        $accountPaginator = new LengthAwarePaginator($items, $accountPaginator->total(), $accountPaginator->perPage(), $accountPaginator->currentPage());

        //格式化
        $keyMap = [
            'id' => 'id',
            'account' => 'account',
            'name' => 'name',
            'phone' => 'phone',
            'nickname' => 'nickname',
            'roleName' => 'roleName',
            'role_id' => 'role_id',
            'create_time' => 'create_time',
            'validity_time' => 'validity_time',
            'state' => 'state'
        ];
        return $this->success(ResponseFormatter::formatterPaginator($accountPaginator, $keyMap));
    }

    //更改用户信息
    public function updateAccount(UpdateAccountPost $request, AccountService $accountService)
    {
        $param = $request->validated();

        $res = $accountService->updateAccount($param);

        return $this->success($res);
    }

    //用户删除
    public function del(Request $request, AccountService $accountService)
    {
        $res = $accountService->del($request['id']);
        return $this->success($res);
    }

    //用户状态
    public function changeState(Request $request,AccountService $accountService){
        $res = $accountService->changeState($request);
        return $this->success($res);
    }

    //重置密码
    public function ResetPassword(Request $request,AccountService $accountService){
        $data = $accountService->ResetPassword($request['id']);
        return $this->success($data);
    }


}
